﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Net;
using System.IO;

namespace CDHunter.Manager
{
    class ExtractManager
    {
        private static readonly string VolumesCS02 = ConfigurationSettings.AppSettings["CS02Volume"];
        private static readonly string VolumesCS07 = ConfigurationSettings.AppSettings["CS07Volume"];

        /// <summary>
        /// Extract
        /// </summary>
        /// <param name="str"></param>
        /// <param name="token1"></param>
        /// <param name="token2"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string Extract(String str, String token1, String token2, int count)
        {
            int location1, location2;

            location1 = location2 = 0;
            do
            {
                location1 = str.IndexOf(token1, location1 + 1);

                if (location1 == -1)
                    return null;

                count--;
            } while (count > 0);

            location2 = str.IndexOf(token2, location1 + 1);
            if (location2 == -1)
                return null;

            location1 += token1.Length;
            return str.Substring(location1, location2 - location1);
        }

        /// <summary>
        /// GetHardDriverVolume
        /// </summary>
        /// <param name="content"></param>
        /// <param name="token1"></param>
        /// <param name="token2"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string GetHardDriverVolume(Uri uri, string token1, string token2, int count)
        {
            String content = ExtractManager.DownloadPage(uri);

            string volume = Extract(content, token1, token2, count);

            if (volume == null || volume.Length >= 20)
                volume = string.Empty;

            return volume;
        }

        /// <summary>
        /// GetProductEnvironmentVersion
        /// </summary>
        /// <param name="pageContent"></param>
        /// <returns></returns>
        public static string GetProductEnvironmentVersion(Uri uri, string volumnToken1, string volumnToken2, int volumeCount)
        {
            String volume = GetHardDriverVolume(uri, volumnToken1, volumnToken2, volumeCount);

            if (!String.IsNullOrEmpty(volume) && VolumesCS02.Contains(volume))
                return "CS02";
            else if (!String.IsNullOrEmpty(volume) && VolumesCS07.Contains(volume))
                return "CS07";
            else
                return "NA";
        }

        /// <summary>
        /// DownloadPage
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string DownloadPage(Uri url)
        {
            WebRequest http = HttpWebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)http.GetResponse();
            StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);

            String result = stream.ReadToEnd();

            response.Close();
            stream.Close();
            return result;
        }
    }
}
